﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace Galaktika.BI.Extensibility
{
    using Galaktika.BI.Extensibility.Dom;
    using Galaktika.BI.Extensibility.Dom.Workstation;

    [XmlRoot(Namespace=Galaktika.BI.Silverlight.Common.Namespace)]
    public class MetaContainer
    {
        public MetaContainer()
        {
        }

        public MetaContainer(IEnumerable<BrickWorkstation> workstations)
        {
            m_Workstations.AddRange(workstations);
        }

        private List<BrickWorkstation> m_Workstations = new List<BrickWorkstation>();
        [XmlArray]
        [XmlArrayItem(typeof(BrickWorkstation))]
        public List<BrickWorkstation> Workstations
        {
            get
            {
                return m_Workstations;
            }
        }

        private List<FormToContextWindowBind> m_ContextWindows = new List<FormToContextWindowBind>();
        [XmlArray]
        [XmlArrayItem(typeof(FormToContextWindowBind))]
        public List<FormToContextWindowBind> ContextWindows
        {
            get
            {
                return m_ContextWindows;
            }
        }

        private List<BrickVariable> m_Variables = new List<BrickVariable>();
        [XmlArray]
        [XmlArrayItem(typeof(BrickVariable))]
        public List<BrickVariable> Variables
        {
            get
            {
                return m_Variables;
            }
        }

        public string ToXml()
        {
            return Galaktika.BI.Silverlight.XmlUtility.Obj2XmlStr(this, Galaktika.BI.Silverlight.Common.Namespace);
        }

        public static MetaContainer FromXml(string xml)
        {
            return Galaktika.BI.Silverlight.XmlUtility.XmlStr2Obj<MetaContainer>(xml);
        }

        public static MetaContainer FromStream(Stream stream)
        {
            using (StreamReader sr = new StreamReader(stream))
            {
                string xml = sr.ReadToEnd();
                return FromXml(xml);
            }
        }
    }
}
